<!doctype html>
<html>

	<head>
		<meta charset="utf-8">
		<meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
		<meta name="format-detection" content="telephone=no" />
		<title>首页</title>
		<link rel="stylesheet" type="text/css" href="../c/animate.css" />
		<link rel="stylesheet" type="text/css" href="../c/aui.css" />
		<style>
			html {
			/*height: auto;*/
		}

		body {
			position: relative;
			height: 100%;
			background: url('../i/login.png');
			background-size: cover;
		}

		.sx-content {
			margin: 0 0.75rem;
			color: #fff;
		}

		.sx-h {
			width: 3rem;
			display: inline-block;
			/*height: 3rem;*/
			margin-left: -0.75rem;
		}

		.sx-h img {
			width: 100%;
			padding: 1rem;
		}

		.i {
			margin-top: 2.5rem;
		}

		.i p {
			text-align: center;
			font-size: 0.85rem;
			color: #fff;
			margin-bottom: 1rem;
		}

		.t input {
			position: relative;
			padding: 0.75rem;
			border-radius: 0.15rem;
			font-size: 0.95rem;
			height: auto;
			background: rgba(226, 226, 226, 0.2);
			color: #fff;
			letter-spacing: 1px;
		}

		.t #yzm {
			margin-right: 8rem;
		}

		.t #yzm input {
			border-top-right-radius: 0rem;
			border-bottom-right-radius: 0rem;
		}

		.t div {
			margin-top: 0.5rem;
			position: relative;
		}

		.t {
			margin: 0 1rem;
		}

		input::-webkit-input-placeholder {
			color: rgba(226, 226, 226, 0.5);
			letter-spacing: 1px;
			font-size: 0.85rem;
		}

		.t div span {
			position: absolute;
			right: 0;
			top: 0;
			display: block;
			background: rgba(226, 226, 226, 0.2);
			padding: 0.75rem;
			border-top-right-radius: 0.15rem;
			border-bottom-right-radius: 0.15rem;
			font-size: 0.7rem;
			height: auto;
			color: #fff;
			letter-spacing: 1px;
			margin-right: -8rem;
			color: #999;
			width: 7.5rem;
			text-align: center;
		}

		.s h4 {
			text-align: center;
			opacity: 0.8;
			font-size: 0.75rem;
		}

		.s {
			width: 60%;
			margin: 0 auto;
			margin-top: 3rem;
		}

		.s .aui-card-list-footer {
			margin-top: 0.75rem;
			text-align: center;
		}

		.s .aui-card-list-footer div {
			width: 2.5rem;
		}

		.s .aui-card-list-footer p {
			color: #fff;
			font-size: 0.7rem;
			padding-top: 0.25rem;
		}

		.sx-xieyi {
			text-align: center;
			color: #fff;
			font-size: 0.6rem;
			padding: 0.75rem 0;
			opacity: 0.75;
		}

		.sx-xieyi span {
			color: #FFB300;
			padding: 0 0.15rem;
			text-decoration: underline
		}

		.sx-btn {
			width: 2.5rem;
			margin: 0 auto;
			text-align: center;
			margin-top: 0.5rem;
			opacity: 0.2;
			margin-top: 2rem;
		}
		#mobile span{
			width: 3rem;
			color: #fff;
			position: absolute;
			top: 0;
			left: 0;
			height: 100%;
			border-top-left-radius: 0.15rem;
			border-bottom-left-radius: 0.15rem;
			font-size: 0.75rem;
			background: none;
			border-right: 1px solid rgba(226, 226, 226, 0.2);
		}
		#mobile input{
			/*padding-left: 4rem;*/
		}
             .yqma{
             	 position: fixed;
             	 width: 90%;
             	 left: 5%;
             	 right: 5%;
             	 border-radius: 0.5rem;
             	 top: 25%;
             	 background:#32376C;
              }
              .yqma p{
             	 width: 100%;
             	 color:#ffffff;
             	     padding-top: 1rem;
             	  text-indent: 3%;
             	  padding-top: 0.5rem;
             	} 
             	.yqma input{
             		width: 94%;
             		margin: 0.5rem 0;
             		border-radius: 0.3rem;
             		color: #fff;
             		height: 2rem;
             		border: 0.01rem solid #fff;
             		text-indent:0.5rem;
             		background: none;
             		margin-left: 3%;
             		border: 0.01rem solid #fff;
             	}
             .yqma button{
             	background: #FE2C55;
             	width: 40%;
             	text-align: center;
             	/* margin-left: 10%; */
             	margin: 0 auto;
             	padding: 0.55rem 0;
             	border-radius: 0.25rem;
             	margin-top: 1rem;
             	font-size: 0.75rem;
             	margin-left: 30%;
             	margin-bottom: 1rem;
             	color: #fff;
             	border: none;
             }	
             	.tcsyqm{
             		position: absolute;
                     top: 0;
             		right: 0;
             		bottom: 0;
             		left: 0;
             		width: 100%;
             		background: rgb(19,19,19);
             		
             
             	}
	</style>
	</head>

	<body>
		<div class="sx-content">
			<div class="sx-h" tapmode onclick="window.location.href='new_index.html'">
				<img src="../i/x.png" />
			</div>
			<div class="i">
				<p>登 录</p>
				<div class="t">

					<div id="mobile">
						<input type="tel" id="mobileinput" placeholder="输入手机号码" value="" />
						<!-- <span data-quhao="86">+86</span> -->
					</div>
					<div id="yzm">
						<input type="tel" id="codeinput" placeholder="输入验证码" value="" />
						<span class="sx-login-code-c" tapmode onclick="code()">获取验证码</span>
					</div>
					<!-- <div id="yqm">
					<input type="tel" id="yqm" placeholder="邀请码（可选）" value="" />
				</div> -->
				</div>
			</div>
			<div class="sx-btn" tapmode onclick="_login();">
				<img src="../i/login_btn.png" />
			</div>

			<div class="s">
				<h4>第三方登录</h4>
				<div class="aui-card-list-footer">
					<div tapmode onclick="_loginTyle('wx')"><img src="../i/share/session.png" />
						<p>微信</p>
					</div>

					<div tapmode onclick="_loginTyle('dy')"><img src="../i/share/douyin.png" />
						<p>抖音</p>
					</div>

					<!-- <div tapmode onclick="_loginTyle('qq')"><img src="../i/share/QFriend.png" />
						<p>QQ</p>
					</div> -->
					<!-- <div tapmode onclick="_loginTyle('wb')"><img src="../i/share/weibo.png" />
						<p>微博</p>
					</div> -->
				</div>
			</div>
			<p class="sx-xieyi">
				登录即代表您已阅读并同意<span tapmode onclick="_page('用户协议',1)">用户协议</span>和<span tapmode onclick="_page('隐私政策',2)">隐私政策</span>
			</p>
		</div>
		<div class="yqma tcsyqm" style="display: none;">
			<div class="yqma">
				<p style="border: 0.01rem solid #fff; border-radius: 0.3rem;">请输入邀请人的邀请码</p>
				<input type="text" placeholder="邀请码" class="yamiput" />
				<button onclick="surebtn()" class="surebtn" style="height: 3rem;">确定</button>

			</div>
		</div>

	</body>

	<script type="text/javascript" src="../j/api.js"></script>
	<script src="../j/jquery-3.3.1.min.js"></script>
	<script type="text/javascript">
		var moduleSMSSDK;
		var fromUid = 0;

		function urlsa() {
			var imglen = $("img").length;
			var picurla = picurl + "/share";
			for (var i = 0; i < imglen; i++) {
				var tpl = $("img").eq(i).attr("src");
				tpla = tpl.replace("../i/share", picurla);
				$("img").eq(i).attr("src", tpla);
			}
		}
		apiready = function() {
			urlsa();
			if (api.systemType == 'ios') {
				$api.css($api.dom('body'), "background: url('../i/login.png') no-repeat center center;background-size: cover;");
			}
			// console.log(_config.mob)
			if (_getPrefs('_config').mob == 1) {
				moduleSMSSDK = api.require('smssdk');
			}

			$('body').css('height', api.winHeight + 'px')
			if (_getPrefs('_config').config.app == 1) {
				$('.s').addClass('aui-hide');
			}
			api.parseTapmode();
			$api.fixStatusBar($api.dom('.sx-h'));
			// 将相应的输入框获取焦点
			setTimeout(function() {
				document.getElementById("mobileinput").focus()
			}, 500)

			// onshow
			api.addEventListener({
				name: 'viewappear'
			}, function(ret, err) {
				// sharetrace获取APP落地页下载参数
				var sharetrace = api.require('sharetrace');
				sharetrace.getInstallTrace(function(ret) {
					// api.alert({msg:JSON.stringify(ret)});
					console.log(JSON.stringify(ret));
					if (ret.code == 200) {
						if (ret.data.paramsData) {
							var paramsData = ret.data.paramsData.split('=')
							fromUid = paramsData[1]
							console.log('fromUid：' + fromUid)
						}
					}
				});
			});

		}

		function yes() {
			var mobile = $('#mobile input').val();
			mobile = $api.trimAll($api.trim(mobile));
			var yzm = $('#yzm input').val();
			if (_isMobile(mobile)) {
				var _this = $('#yzm span');
				_this.css('color', '#fff');
				// if(_this.css('color') == 'rgb(255, 255, 255)'){
				// 	 console.log(_this.css('color'))
				// 	_this.addClass('shake');
				// 	setTimeout(function(){
				// 		 _this.removeClass('shake');
				// 	}, 1000);
				// }
				if (yzm && yzm.length >= 4) {
					// console.log(yzm.length)
					var _this = $('.sx-btn');
					if (_this.css('opacity') == 1) {
						return;
					}
					_this.css('opacity', '1');
					_this.addClass('animated rubberBand');
					setTimeout(function() {
						_this.removeClass('animated rubberBand');
					}, 1000);
					return;
				}
			} else {
				$('#yzm span').css('color', '#999');
			}

			$('.sx-btn').css('opacity', '0.2')

		}
		$("#codeinput").bind("input propertychange", function() {
			yes();
		});
		$("#mobileinput").bind("input propertychange", function() {
			yes();
		});

		function _isMobile(phone) {
			if (!(/^1[123456789]\d{9}$/.test(phone))) {
				return false;
			} else {
				return true;
			}
		}
		var isinerval
		// 发送验证码
		function code() {
			if (times) {
				return _msg('不要重复操作')
			}
			_loading();
			var mobile = $('#mobile input').val();
			mobile = $api.trimAll($api.trim(mobile));
			if (_isMobile(mobile)) {
				if (_getPrefs('_config').mob == 1) {
					var param = {
						zone: '86',
						phoneNumber: mobile,
						tempCode: _getPrefs('_config').sdkReg_id
					};
					moduleSMSSDK.getTextCode(param, function(ret, err) {
						console.log(JSON.stringify(ret) + JSON.stringify(err));
						if (err !== null && err !== undefined && err !== '') {
							_loadingCloes();
							console.log("_config.sdkReg_id:" + _getPrefs('_config').sdkReg_id)
							// 错误消息示例：{"msg":"Template not exist.","code":484}
							_suxiangw_msg2({
								content: err.msg,
								rightTitle: '知道了',
							}, function(ret) {
								dialogBox.close({
									dialogName: 'alert'
								});
							});
							return
							console.log("Error:\n" + JSON.stringify(err));
						} else {
							_loadingCloes();
							_msg('验证码已发送')
							// 正常消息示例：{"smart":false}
							console.log("Success:\n" + JSON.stringify(ret));
							$('.sx-login-code-c').html('重新获取(120s)');
							times = 120;
							$('.sx-login-code-c').css('color', '');
							isinerval = setInterval("CountDown()", 1000);
							document.getElementById("codeinput").focus()
						}
					});
					return;
				}


				_ajax('user/code', function(ret, err) {
					// console.log(JSON.stringify(ret));
					_loadingCloes();
					if (ret) {
						// console.log(JSON.stringify(ret));
						if (ret.ret) {
							$('.sx-login-code-c').html('重新获取(120s)');
							times = 120;
							$('.sx-login-code-c').css('color', '');
							isinerval = setInterval("CountDown()", 1000);
							document.getElementById("codeinput").focus()
						} else {
							_suxiangw_msg2({
								content: ret.err,
								rightTitle: '知道了',
							}, function(ret) {
								dialogBox.close({
									dialogName: 'alert'
								});
							});
							return
						}
					} else {
						return _alert(JSON.stringify(err));
					}
				}, {
					username: mobile,
					text: '登录'
				})
			} else {
				_msg('手机号码格式错误');
				document.getElementById("mobileinput").focus()
			}
		}
		var times = 0;

		function CountDown() {
			times--;
			if (times < 1) {
				$('.sx-login-code-c').html('获取验证码');
				$('.sx-login-code-c').css('color', '#fff');
				clearInterval(isinerval);
				return;
			}
			$('.sx-login-code-c').html('重新获取(' + times + 's)');
		}
		// 登录
		function _login() {
			var mobile = $('#mobile input').val();
			mobile = $api.trimAll($api.trim(mobile));
			var yzm = $('#yzm input').val();
			if (!mobile) {
				_msg('请输入手机号码');
				return document.getElementById("mobileinput").focus()
			}
			if (!_isMobile(mobile)) {
				_msg('手机号码格式错误');
				return document.getElementById("mobileinput").focus()
			}
			if (!yzm) {
				document.getElementById("codeinput").focus()
				return _msg('请输入验证码');
			}
			if (yzm.length < 4) {
				document.getElementById("codeinput").focus()
				return _msg('请输入正确验证码')
			}
			document.activeElement.blur();
			_loading();
			// if (_getPrefs('_config').mob == 1) {
			// 	var param = {
			// 		zone: '86',
			// 		phoneNumber: mobile,
			// 		code: yzm
			// 	};
			// 	moduleSMSSDK.commitCode(param, function(ret, err) {
			// 		if (err !== null && err !== undefined && err !== '') {
			// 			// 错误消息示例：{"msg":"Template not exist.","code":484}
			// 			return _msg("code:"+err.code+"，"+err.msg);
			// 		} else {
			// 			console.log(JSON.stringify(ret))
			// 		}
			// 	});
			// 	_loadingCloes();
			// 	return;
			// }

			_ajax('user/login', function(ret, err) {
				console.log(JSON.stringify(ret));
				if (ret) {
					if (ret.ret) {
						localStorage.setItem("token", ret.ret);
						_setPrefs('_token', ret.ret);
						$(".surebtn").attr("name", ret.ret);
						// 当前用户绑定状态
						var bindStatus = ret.bind;
						// 获取用户资料
						_ajax('member/info', function(ret, err) {
							console.log(JSON.stringify(ret));
							_loadingCloes();
							if (ret) {
								if (ret.err) {

								} else {
									// _setPrefs('_user', ret);
									// api.execScript({
									// 	name: 'root',
									// 	script: 'rongyun_init()'
									// });
									// 
									// 如果当前用户没有被绑定并且有邀请用户主键id
									// bindUser(356);
									if (bindStatus == 0) {
										// alert("sss");
										$(".yqma").show();
										// console.log('执行绑定进来了吗？')

									} else {
										_setPrefs('_user', ret);
										bindUser(fromUid);
										// alert("sssa");
										// _close();
										// window.location.href = "new_my.html";
									}

								}
							}
						})

					} else {
						_loadingCloes();
						return _msg(ret.err)
					}
				} else {
					_loadingCloes();
					_alert(JSON.stringify(err))
				}
			}, {
				mobile: mobile,
				code: yzm
			})
		}

		function surebtn() {
			var token = $(".surebtn").attr("name");
			var yam = $(".yamiput").val();
			if (yam == "") {
				_msg("邀请码不能为空");
			} else {
				yam=yam.replace(" ","");
				_ajax('renwu/yaoqing_code', function(ret, err) {
					// console.log(JSON.stringify(ret));
					if (ret.code == "0" || ret.code == 0 && ret.code != undefined) {
						_msg(JSON.stringify(ret.ret));
						_ajax('member/info', function(ret, err) {
							console.log(JSON.stringify(ret));
							_loadingCloes();
							if (ret) {
								if (ret.err) {} else {
									_setPrefs('_user', ret);
									// _close();
									window.location.href = "new_my.html";
								}
							}
						})

					} else {
						_msg(JSON.stringify(ret.err))
					}

				}, {
					yq_code: yam
				})
			}

		}
		// 请求绑定上下级
		function bindUser(fromUid) {
			_ajax('renwu/yaoqing_yqm', function(ret, err) {
				// _close();
			}, {
				from_uid: fromUid
			})
			window.location.href = "new_my.html";
		}
		// 第三方登陆
		function _loginTyle(type) {
			document.activeElement.blur();
			if (type == 'wx') {
				var wx = api.require('wxPlus');
				wx.isInstalled(function(ret, err) {
					if (ret.installed) {
						wx.auth({}, function(reta, erra) {
							if (reta.status) {
								wx.getToken({
									code: reta.code
								}, function(retg, errg) {
									if (retg.status) {
										wx.getUserInfo({
											accessToken: retg.accessToken,
											openId: retg.openId
										}, function(retw, errw) {
											if (retw.status) {
												if (retw.openid) {
													_loginToken({
														type: 'wxapp',
														openid: retw.openid,
														unionid: retw.unionid,
														img: retw.headimgurl,
														nickname: retw.nickname,
														info: retw
													})
												} else {
													_loadingCloes();
													_alert('openId值为空')
												}
											} else {
												_loadingCloes();
											}
										});
									} else {
										_loadingCloes();
									}
								});
							} else {
								_loadingCloes();
							}
						});
					} else {
						_loadingCloes();
						_alert('当前设备未安装微信客户端')
					}
				});
			} else if (type == 'qq') {
				var qq = api.require('QQPlus');
				qq.installed(function(ret, err) {
					if (ret.status) {
						qq.login(function(retl, errl) {
							if (retl.status) {
								//		console.log(JSON.stringify(retl))
								qq.getUserInfo(function(retg, errg) {
									var openid = retl.openId;
									if (openid && retg.status) {
										if (api.systemType == 'ios') {

										} else {
											retg.info = eval('(' + retg.info.replace(/}{/, ',') + ')');
										}
										//		console.log(retg.info);
										_loginToken({
											type: 'qqapp',
											openid: openid,
											unionid: retg.unionid,
											img: retg.info.figureurl_qq_2,
											nickname: retg.info.nickname,
											info: retg.info
										})
									} else {
										_loadingCloes();
										_alert('获取QQ资料失败')
									}
								});
							} else {
								_loadingCloes();
							}
						});
					} else {
						_loadingCloes();
						_alert('当前设备未安装QQ客户端')
					}
				});
			} else if (type == 'wb') {
				var weibo = api.require('weiboPlus');
				weibo.isInstalled(function(ret) {
					console.log(JSON.stringify(ret));
					_loadingCloes();
					if (ret.status) {
						weibo.auth(function(reta, erra) {
							if (reta) {
								if (reta.status) {
									weibo.getUserInfo({
										token: reta.token,
										userId: reta.userId
									}, function(retg, errg) {
										if (retg.status) {
											retg.userInfo = JSON.parse(retg.userInfo);
											var openid = retg.userInfo.id
											if (openid) {
												_loginToken({
													type: 'wbapp',
													openid: openid,
													img: retg.userInfo.avatar_hd,
													nickname: retg.userInfo.name,
													info: retg.userInfo
												})
												//	_login(openid, 'wbapp')
											} else {
												_loadingCloes();
												_alert('openId值为空')
											}
											return;
										} else {
											_loadingCloes();
										}
									});
								} else {
									_loadingCloes();
								}
							} else {
								_loadingCloes();
							}
						});
					} else {
						api.alert({
							msg: '当前设备未安装新浪微博客户端'
						});
						return
					}
				});
			} else if (type == 'dy') {
				var dyInit = api.require('DouYinPlatform');
				dyInit.auth({
					'scope': 'user_info'
				}, function(ret) {
					if (ret) {
						// alert(JSON.stringify(ret));
						console.log(JSON.stringify(ret));
						if (ret.code == 0) {

							_ajax1('douyin/userinfo', function(ret, err) {

								if (ret.ret) {

									_loginToken({
										type: 'dyapp',
										openid: ret.ret.data.open_id,
										unionid: ret.ret.data.union_id || '',
										img: ret.ret.data.avatar,
										nickname: ret.ret.data.nickname,
										info: ret.ret
									})

								} else {
									api.alert({
										msg: ret.err
									});
								}

							}, ret);

						} else {
							api.alert({
								msg: '您取消了抖音授权'
							});
							return;
						}
					}
				});

			}
		}

		// 封装ajax登陆获取token
		function _loginToken(data) {
			console.log(JSON.stringify(data))
			_loading()
			_ajax('user/oauth', function(ret, err) {

				console.log(JSON.stringify(ret) + JSON.stringify(err))

				// 当前用户绑定状态
				var bindStatus = ret.bind;
				data.bindStatus = bindStatus
				data.fromUid = fromUid

				if (ret) {
					if (ret.mobile) {
						_loadingCloes()
						// 跳转绑定手机
						localStorage.setItem("bind", ret.bind);
						_url({
							url: 'login_mobile',
							title: '绑定手机',
							data: data
						})
						return;
					}
					_setPrefs('_token', ret.ret);
					// 获取用户资料
					_ajax('member/info', function(ret, err) {
						_loadingCloes();
						if (ret) {
							if (ret.err) {

							} else {
								_setPrefs('_user', ret);
								api.execScript({
									name: 'root',
									script: 'rongyun_init()'
								});
								api.execScript({
									name: 'root',
									frameName: 'scrollVideo',
									script: 'yuandu();'
								});
								window.location.href = "new_my.html";
							}
						}
					})


					console.log(JSON.stringify(ret))
				} else {
					_loadingCloes()
					console.log(JSON.stringify(err))
					_alert(err)
				}
			}, data)
		}
	</script>

</html>
